package com.atguigu.lease.web.admin.service.impl;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {


    @Autowired
    private GraphInfoService graphInfoService;


    @Autowired
    private RoomAttrValueService attrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService labelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService leaseTermService;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper ;
    @Autowired
    private GraphInfoMapper graphInfoMapper ;
    @Autowired
    private AttrValueMapper attrValueMapper ;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper ;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public void RoomSaveOrUpdate(RoomSubmitVo roomSubmitVo) {


        boolean isUpdate = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);


        //如果是修改操作，则删除room相关的信息列表
        if (isUpdate) {
            //1、删除graphInfoList
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoService.remove(graphQueryWrapper);


            //2、删除attrValueIds
            LambdaQueryWrapper<RoomAttrValue> attrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            attrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            attrValueService.remove(attrValueLambdaQueryWrapper);

            //3、删除facility
            LambdaQueryWrapper<RoomFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(facilityLambdaQueryWrapper);

            //4、删除label
            LambdaQueryWrapper<RoomLabel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            labelService.remove(lambdaQueryWrapper);

            //5、删除roompayment
            LambdaQueryWrapper<RoomPaymentType> paymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            paymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(paymentTypeLambdaQueryWrapper);

            //6、删除roomlease
            LambdaQueryWrapper<RoomLeaseTerm> leaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            leaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            leaseTermService.remove(leaseTermLambdaQueryWrapper);
        }

        //新增graphInfolist
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();

        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphlist = new ArrayList<>();

            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();

                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());

                graphlist.add(graphInfo);

            }
            graphInfoService.saveBatch(graphlist);
        }
        //2、新增attrvalue
        List<Long> attrvaluesIdList = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrvaluesIdList)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrvalueId : attrvaluesIdList) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrvalueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            attrValueService.saveBatch(roomAttrValueList);
        }

        //3、新增facilityList
        //从 roomSubmitVo 对象中获取设施信息的 ID 列表。假设这个列表包含了需要添加到数据库中的设施信息的 ID。
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        //检查设施信息的 ID 列表是否为空。如果列表不为空，则执行后续操作；否则，跳过。
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            //创建一个空的 RoomFacility 对象列表，用于存储即将添加到数据库中的房间设施信息。
            List<RoomFacility> facilityList = new ArrayList<>();
            //遍历设施信息的 ID 列表
            for (Long facility : facilityInfoIds) {
                //根据每个设施信息的 ID 创建一个 RoomFacility 对象，并设置该对象的 roomId 和 facilityId 属性。
                // 这里使用了建造者模式创建 RoomFacility 对象，传入了房间的 ID 和设施的 ID。
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facility).build();
                //将创建好的 RoomFacility 对象添加到房间设施信息列表中。
                facilityList.add(roomFacility);
            }//调用 roomFacilityService 的 saveBatch 方法，将房间设施信息列表批量保存到数据库中。
            roomFacilityService.saveBatch(facilityList);
        }
        //4、新增labelinfolist
        //先获取前端传过来的数据，获取其中的id
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        //如果传过来的数据不为空
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            //创建一个roomlabellist集合保存即将添加到
            List<RoomLabel> labelList = new ArrayList<>();
            //使用for循环遍历labelInfoIds，将里面的数据提取出来，保存到labelList
            for (Long label : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(label).build();
                labelList.add(roomLabel);
            }
            labelService.saveBatch(labelList);
        }

        //5、新增payment
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {

            List<RoomPaymentType> paymentTypesList = new ArrayList<>();

            for (Long payment : paymentTypeIds) {
                RoomPaymentType build = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(payment).build();
                paymentTypesList.add(build);
            }
            roomPaymentTypeService.saveBatch(paymentTypesList);
        }

        //6、新增lease
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
            for (Long lease : leaseTermIds) {
                RoomLeaseTerm build = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(lease).build();
                roomLeaseTermList.add(build);
            }
            leaseTermService.saveBatch(roomLeaseTermList);
        }
    }

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDate(Long id) {

        RoomInfo roomInfo = roomInfoMapper.selectById(id);

        //查询apartmentInfo
        apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        //查询graphVoList
        //lambdaQuery创建一个LambdaQueryWrapper 对象，用于构建查询条件。
        List<GraphVo> graphInfoList = graphInfoService.lambdaQuery()
                //eq（）方法表示 等于=；用于添加查询条件
                .eq(GraphInfo::getItemId, id)
                //例如，判断GraphInfo::getItemType 是否等于 ItemType.ROOM
                .eq(GraphInfo::getItemType, ItemType.ROOM).list()
                //将满足以上条件的数据 放进 流Stream
                .stream()
                //map() 方法将每个 GraphInfo 对象映射成对应的 GraphVo 对象。
                .map(graphvo -> new GraphVo(graphvo.getName(), graphvo.getUrl())).toList();


        //查询属性信息列表AttrValue
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueByRoomId(id);

        //查询facility配套信息表
        List<FacilityInfo> fafilityList = facilityInfoMapper.selectFacilityByRoomId(id);

        //查询label标签信息表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelByRoomId(id);

        //查询支付方式信息表PaymentType
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentById(id);

        //查询租期信息表leaseterm
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectTermById(id);

        //用于复制一个 Java Bean 对象的属性到另一个 Java Bean 对象中
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setGraphVoList(graphInfoList);
        //attrValueVoList赋值给roomDetailVo
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        //添加facility
        roomDetailVo.setFacilityInfoList(fafilityList);
        //添加label
        roomDetailVo.setLabelInfoList(labelInfoList);
        //添加payment
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        //添加leaseterm
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;

    }

}




